package com.ecommerce.common.core;

import lombok.Builder;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Data
@Builder
public class PageResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer pageNum;
    private Integer pageSize;
    private Long total;
    private List<T> list;
    private Integer totalPage;
    private Boolean hasNextPage;
    private Boolean hasPreviousPage;
    public PageResult() {}
    public PageResult(Integer pageNum, Integer pageSize, Long total, List<T> list) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
        this.list = list;
        this.totalPage = (int) Math.ceil((double) total / pageSize);
        this.hasNextPage = pageNum < totalPage;
        this.hasPreviousPage = pageNum > 1;
    }

    public PageResult(Integer pageNum, Integer pageSize, Long total, List<T> list, Integer totalPage, Boolean hasNextPage, Boolean hasPreviousPage) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
        this.list = list;
        this.totalPage = totalPage;
        this.hasNextPage = hasNextPage;
        this.hasPreviousPage = hasPreviousPage;
    }

    public static <T> PageResult<T> of(Integer pageNum, Integer pageSize, Long total, List<T> list) {
        return new PageResult<T>(pageNum, pageSize, total, list);
    }

    public static <T> PageResult<T> empty(Integer pageNum, Integer pageSize) {
        return new PageResult<T>(pageNum, pageSize, 0L, null);
    }
}